import { ref } from 'vue';
import type { Category } from '@/api/product/attr/type';
import { reqGetNextLevelCategory } from '@/api/product/attr';
export const useCategory = () => {
	// 一级分类
	let firstLevel = ref<Category[]>([]);
	// 二级分类
	let secondLevel = ref<Category[]>([]);
	// 三级分类
	let thirdLevel = ref<Category[]>([]);
	/**
	 * 根据分类id获取下一级分类id，不传参获取一级分类列表
	 * @param id category_id
	 * @returns 下一级分类列表(Promise)
	 */
	let getNextLevelCategory = async (id: number = 0): Promise<Category[]> => {
		let { code, message, data } = await reqGetNextLevelCategory(id);
		if (code != 200) {
			ElMessage.error({ message: message });
			return Promise.reject();
		}
		return data;
	};

	/**
	 * 为secondLevel赋值
	 * @param id
	 */
	let setFirstLevelCategory = async () => {
		firstLevel.value = await getNextLevelCategory();
	};
	/**
	 * 为secondLevel赋值
	 * @param id
	 */
	let setSecondLevelCategory = async (id: number) => {
		secondLevel.value = await getNextLevelCategory(id);
	};
	/**
	 * 为secondLevel赋值
	 * @param id
	 */
	let setThirdLevelCategory = async (id: number) => {
		thirdLevel.value = await getNextLevelCategory(id);
	};
	return {
		firstLevel,
		secondLevel,
		thirdLevel,
		getNextLevelCategory,
		setFirstLevelCategory,
		setSecondLevelCategory,
		setThirdLevelCategory,
	};
};
